package cn.neusoft.luofa603.yuejian_29.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.List;
import cn.neusoft.luofa603.yuejian_29.Global;
import cn.neusoft.luofa603.yuejian_29.R;
import cn.neusoft.luofa603.yuejian_29.bean.Club;

public class ClubSearchAdapter  extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List mDataList;
    private LayoutInflater mLayoutInflater;
    private MyItemClickListener mItemClickListener;

    public  ClubSearchAdapter(Context mContext, List mDataList){
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mLayoutInflater.from(parent.getContext()).inflate(R.layout.item_club_search,parent,false);
        ClubSearchAdapter.ViewHolder holder = new  ClubSearchAdapter.ViewHolder(v,mItemClickListener);
        return holder;
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        ImageView club_pic;
        TextView club_name,hits,comtact;
        private  ClubSearchAdapter.MyItemClickListener mListener;

        public ViewHolder(View itemView, ClubSearchAdapter.MyItemClickListener myItemClickListener) {
            super(itemView);
            club_pic = (ImageView)itemView.findViewById(R.id.club_img);
            club_name = (TextView)itemView.findViewById(R.id.club_name);
            hits=(TextView)itemView.findViewById(R.id.hits);
            comtact=(TextView)itemView.findViewById(R.id.contact) ;


            this.mListener = myItemClickListener;
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            if (mListener != null) {
                mListener.onItemClick(view, getPosition());
            }

        }
    }



    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final Club entity = (Club) mDataList.get(position);
        if (null==entity)
            return;
        ClubSearchAdapter.ViewHolder viewHolder = ( ClubSearchAdapter.ViewHolder) holder;
        viewHolder.club_name.setText(entity.getClub_name());
        viewHolder.hits.setText(entity.getHits());
        viewHolder.comtact.setText(entity.getClub_contact());

        String photo = entity.getThumb();
        Picasso.with(mContext)
                .load(Global.PIC+photo)
                .into(viewHolder.club_pic);

    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    public interface MyItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setItemClickListener( ClubSearchAdapter.MyItemClickListener myItemClickListener){
        this.mItemClickListener = myItemClickListener;
    }
}
